//
//  QDCodePopOverTableView.m
//  QDMetroClient
//
//  Created by YueWen on 2019/1/18.
//  Copyright © 2019 YueWen. All rights reserved.
//

#import "QDCodePopOverTableView.h"
#import "NWTableViewCell.h"

/// 展示在 QDCodePopOverTableView 的tableViewCell
@interface QDCodePopOverTableViewCell: NWTableViewCell
/// 展示的图标
@property (nonatomic, strong) UIImageView *imageView;
/// 展示的标题
@property (nonatomic, strong) UILabel * titleLabel;

@end

@implementation QDCodePopOverTableViewCell

@synthesize imageView = _imageView;

- (void)buildView {
    [super buildView];
    
    self.imageView = ({
        
        UIImageView *imageView = [UIImageView new];
        imageView.backgroundColor = UIColor.whiteColor;
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        
        imageView;
    });
    
    self.titleLabel = ({
        
        UILabel *label = [UILabel new];
        label.backgroundColor = [UIColor whiteColor];
        label.text = @"";
        label.font = RITLRegularFont(14);
        label.textColor = QDSColorDefaultBlack;
        
        label;
    });
    
    [self.contentView addSubview:self.imageView];
    [self.contentView addSubview:self.titleLabel];
    
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.top.offset(7);
        make.width.height.mas_equalTo(24);
//        make.height.mas_equalTo(17);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.imageView.mas_right).offset(4);
        make.top.offset(9);
        make.height.mas_lessThanOrEqualTo(20);
    }];
}

@end


@interface QDCodePopOverTableView () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, copy)NSArray <NSString *> *titles;
@property (nonatomic, copy)NSArray <NSString *> *imageNames;

@end

@implementation QDCodePopOverTableView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = UIColor.whiteColor;
        self.tableView = ({
            
            UITableView *view = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
            view.contentInset = UIEdgeInsetsMake(7, 0, 7, 0);
            view.rowHeight = 38;
            view.tableFooterView = UIView.new;
            view.separatorStyle = UITableViewCellSeparatorStyleNone;
            view.showsVerticalScrollIndicator = false;
            view.showsHorizontalScrollIndicator = false;
            view.dataSource = self;
            view.delegate = self;
            view.scrollEnabled = false;
            
            [view registerClass:QDCodePopOverTableViewCell.class forCellReuseIdentifier:@"QDCodePopOverTableViewCell"];
            
            view;
        });
        
        [self addSubview:self.tableView];
        
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.bottom.right.offset(0);
        }];
        
        self.titles = @[];
        self.imageNames = @[];
    }
    
    return self;
}

#pragma mark - <UITableViewDataSource>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    QDCodePopOverTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QDCodePopOverTableViewCell" forIndexPath:indexPath];
    
    //进行图片以及title的赋值
    cell.titleLabel.text = self.titles[indexPath.row];
    cell.imageView.image = self.imageNames[indexPath.row].ritl_image;
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [self.delegate codePopOverTableView:self didSelected:self.titles[indexPath.row] index:indexPath.row];
}


- (void)updateTitles:(NSArray<NSString *> *)titles images:(NSArray<NSString *> *)imageNames {
    
    NSAssert(titles.count == imageNames.count, @"titles的个数与imageNames的个数必须一致");
    
    //如果个数不相同，修改高度
    if (titles.count != self.titles.count) {
        self.ritl_height = [[self class] heightForPopOverTableViewWithCount:titles.count];
    }
    
    self.titles = titles;
    self.imageNames = imageNames;
    
    //进行刷新
    [self.tableView reloadData];
}


+ (CGFloat)heightForPopOverTableViewWithCount:(NSInteger)count {
    
    CGFloat cellHeight = 38;
    CGFloat totalCellHeight = cellHeight * count;
    return totalCellHeight + 7 + 7/*bottomInset*/;
}


@end
